package com.link.base.base.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.link.base.wechat.wxtoken.util.WxTokenUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信工具类
 *
 * @author 刘永枭
 */
@Controller
@RequestMapping("/link/wechartUtil")
public class WechatUtilController {

    @RequestMapping(value = "getAccessToken", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAccessToken(@RequestBody JSONObject jsonObject) {
        Map<String, Object> result = new HashMap<>(3);
        try {
            String appId = (String) jsonObject.get("appId");
            String appSecret = (String) jsonObject.get("appSecret");
            String token = WxTokenUtil.getAccessTokenBySecret(appId, appSecret);
            result.put("token", token);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
